<?php
session_start(); //we need to call PHP's session object to access it through CI
class materi extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->data['menu'] = 'materi';
		$this->data['form'] = 'custom/form_materi';
		$this->data['title'] = 'Materi';
	}

	function index()
	{
		$admin = $this->session->userdata('admin');
		$params = array('tablename' => 'materi', 'primary_key' => 'mat_id', 'update'=>false, 'where'=>array('ad_id'=>$admin['id']));
		$this->load->library('grid', $params);
		$this->grid->add_subject('Materi');
		$this->grid->add_field('mat_name', 'Judul Materi', '', 'show');
		$this->grid->add_relation_field('mp_name', 'mata_pelajaran', 'mp_id', 'Mata Pelajaran', '', 'show');
		$this->grid->set_menu('materi');
		$this->grid->set_form('form_materi');
		$this->grid->set_portlet_style('portlet box green');
		$this->grid->set_portlet_title('Data Materi');
		$this->grid->show();
	}

	function form_value(){
		$value['mat_name'] = $this->input->post('mat_name');
		$value['mp_id'] = $this->input->post('mp_id');
		$name = $_FILES['mat_file']['name'];
		$value['mat_file'] = str_replace(" ", "_", strtolower($name));
		return $value;
	}

	function drop_down_value(){
		$this->load->model('m_mata_pelajaran');
		$this->load->model('m_kelas');
		$this->data['row_mp'] = $this->m_mata_pelajaran->get_data();
		$this->data['kelas'] = $this->m_kelas->get_data();
	}

	function init_data_form(){
		$this->data['mat_name'] = '';
		$this->data['mat_file'] = '';
		$this->data['mp_id'] = '';
		$this->data['classes'] = array();
	}

	function add(){
		$this->init_data_form();
		$this->drop_down_value();
		$this->data['portlet_style'] = 'portlet box red';
		$this->data['portlet_title'] = 'Tambah Data Materi';
		$this->data['action'] = base_url().'materi/action/insert';
		$this->data['portlet_type'] = 'Form';
		$this->data['id'] = '0';
		$this->load();
	}

	function action(){
		$this->load->model('m_materi');
		$this->load->model('m_kelas_materi');

		$act = $this->uri->segment(3);
		$id = $this->input->post('mat_id');
		$value = $this->form_value();
		switch ($act){
			case "insert":
				$upload = $this->upload($value['mat_file']);
				if($upload == "Success"){
					$admin = $this->session->userdata('admin');
					$value['ad_id'] = $admin['id'];
					$res = $this->m_materi->insert($value);
					foreach($this->input->post('kelas') as $kelas){
						$data['mat_id'] = $res;
						$data['kl_id'] = $kelas;
						$this->m_kelas_materi->insert($data);
					}
				}
				echo $upload;
				break;
			case "update":
				$res = $this->m_materi->update($value, array('mat_id' => $id));
				$loc = 'materi/update/'.$id;
				break;
		}
	}

	function update($id){
		$this->load->model('m_materi');
		$row = (array)$this->m_materi->get_data_by_id($id);
		$this->data = array_merge($row, $this->data);
		$this->drop_down_value();
		$this->data['portlet_style'] = 'portlet box red';
		$this->data['portlet_title'] = 'Update Data Materi';
		$this->data['action'] = base_url().'materi/action/update';
		$this->data['portlet_type'] = 'Form';
		$this->data['id'] = $id;
		$this->load();
		//print_r($this->data);
	}
	
	function load(){
		if($this->session->userdata('admin')){
			$session_data = $this->session->userdata('admin');
			$this->data['username'] = $session_data['username'];
			$this->data['nama'] = $session_data['nama'];
			$this->data['type'] = $session_data['type'];
			$this->load->view('main', $this->data);
		}else{
			redirect('admin', 'refresh');
		}
	}

	function upload($name){
		$size = $_FILES['mat_file']['size'];
		$valid_formats = array("txt", "doc", "docx", "pdf", "zip", "rar", "mp4", "avi", "xls", "xlsx");
		list($txt, $ext) = explode(".", $name);
		if(strlen($name)<=50){
			if(in_array($ext,$valid_formats)){
				$tmp = $_FILES['mat_file']['tmp_name'];
				if(move_uploaded_file($tmp, MATERI_PATH.$name)) return "Success";
				else return "Upload data gagal";
			}else
				return "Format .".$ext." tidak diperbolehkan";
		}else "Nama file terlalu panjang";
	}
	
	function delete($id){
		$this->load->model('m_materi');
		$res = $this->m_materi->delete(array('mat_id' => $id));
		if(!$res){
			$this->data['error_message'] = 'Gagal';
		}else{
			unlink(MATERI_PATH.$res);
		}
		redirect('materi');
	}
}
?>